package org.traffometer.traffosim;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Creates a controller specifically for the GUI.  Implements the
 * default Controller functionality as well.
 * @author Joshua Stupplebeen
 *
 */
public class UIController {

  /** The model instance which will be updated by Listeners in this class. */
  private final TraffosimModel model;
  /** The view instance which provides the UI functionality. */
  private final View view;
  /** The controller instance passing the instantiated data from the CommandLineUI. */
  private final Controller controller = new Controller();

  /**
   * Create an instance of a Controller that will tie together the passed Model and View.
   * 
   * @param model The model, an instance of StackModel.
   * @param view The View, which will display the StackModel state and support operations on it.
   */
  public UIController(TraffosimModel model, View view) {
    this.model = model;
    model.addObserver(view);
    
    this.view = view;
    this.view.addBrowseListener(new BrowseListener());
    this.view.addClearListener(new ClearListener());
    this.view.addRunListener(new RunListener());
    this.view.showGUI();
    
  }
  
  /**
   * Create an instance of a Controller that will tie together the passed Model and View.
   * 
   * @param model The model, an instance of StackModel.
   * @param view The View, which will display the StackModel state and support operations on it.
   * @param args The command line arguments.
   * @throws ConfigException Indicates an error in the configuration initialization.
   */
  public UIController(TraffosimModel model, View view, String[] args) throws ConfigException {
    this.model = model;
    model.addObserver(view);
    
    this.view = view;
    this.view.addBrowseListener(new BrowseListener());
    this.view.addClearListener(new ClearListener());
    this.view.addRunListener(new RunListener());
    this.view.showGUI();

    // parse command line args
    for (int i = 0; i < args.length; i++) {
        if ("-server".equals(args[i])) {
          controller.setServerAddress(args[i + 1]);
        }
        else if ("-config".equals(args[i])) {
          File configFile = new File(args[i + 1]);
          if (configFile.isFile()) {
            controller.openConfig(configFile);
          }
          else {
            System.err.println("Specified config file does not exist");
          }
        }
    }
  }
  
  /**
   * Implements the Browse capability to select a config file.
   * @author Josh
   *
   */
  public class BrowseListener implements ActionListener {

    /**
     * Responds to button presses.
     * @param e The browse button event.
     */
    public void actionPerformed(ActionEvent e) {
      model.browse();
    }
    
  }

  /**
   * Runs the simulation.
   * @author Josh
   *
   */
  public class RunListener implements ActionListener {

    /**
     * Responds to button presses.
     * @param e The run button event.
     */
    public void actionPerformed(ActionEvent e) {
      model.runSim(controller);
    }
    
  }

  /**
   * Clears the simulation output textField.
   * @author Josh
   *
   */
  public class ClearListener implements ActionListener {

    /**
     * Responds to button presses.
     * @param e The clear button event.
     */
    public void actionPerformed(ActionEvent e) {
      model.clearOutput();
    }
    
  }
  
}
